Date.prototype.format = function (pattern) {
    pattern = pattern || 'yyyy-MM-dd';
    var y = this.getFullYear().toString();
    var o = {
        M: this.getMonth() + 1, //month
        d: this.getDate(), //day
        h: this.getHours(), //hour
        m: this.getMinutes(), //minute
        s: this.getSeconds() //second
    };
    pattern = pattern.replace(/(y+)/ig, function (a, b) {
        return y.substr(4 - Math.min(4, b.length));
    });
    for (var i in o) {
        pattern = pattern.replace(new RegExp('(' + i + '+)', 'g'), function (a, b) {
            return (o[i] < 10 && b.length > 1) ? '0' + o[i] : o[i];
        });
    }
    return pattern;
}